﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Web;

namespace MvcProject.Models
{
    public class BaseModels
    {
        private LongShareDataContext db = new LongShareDataContext();
        //******************************Team****************************************
        // Query Methods
        public IQueryable<Team> FindAllTeams()
        {
            return db.Team.OrderBy(d => d.TeamOrder);
        }
        public IQueryable<Team> FindAllTeamsByProjectId(int projectId)
        {
            return db.Team.Where(d => d.projectId == projectId).OrderBy(d => d.TeamOrder);
        }
        public Team GetTeam(int TeamId)
        {
            return db.Team.SingleOrDefault(d => d.TeamId == TeamId);
        }
        //
        // Insert/Delete Methods
        public void Add(Team dinner)
        {
            db.Team.InsertOnSubmit(dinner);
        }
        public void Delete(Team dinner)
        {
            db.Team.DeleteOnSubmit(dinner);
        }
        public void DeleteAllTeamByProjectId(int projectId)
        {
            db.Team.DeleteAllOnSubmit(FindAllTeamsByProjectId(projectId));
        }
        //******************************WorkType****************************************
        public IQueryable<WorkType> FindAllWorkTypes()
        {
            return db.WorkType.OrderBy(d => d.WorkTypeOrder);
        }
        public WorkType GetWorkType(int WorkTypeId)
        {
            return db.WorkType.SingleOrDefault(d => d.WorkTypeId == WorkTypeId);
        }
        //
        // Insert/Delete Methods
        public void Add(WorkType dinner)
        {
            db.WorkType.InsertOnSubmit(dinner);
        }
        public void Delete(WorkType dinner)
        {
            db.WorkType.DeleteOnSubmit(dinner);
        }
        //******************************TemporaryWorkers****************************************
        // Query Methods
        public IQueryable<TemporaryWorkers> FindAllTemporaryWorkers()
        {
            return db.TemporaryWorkers;
        }
        public TemporaryWorkers GetTemporaryWorker(int TemporaryWorkerId)
        {
            return db.TemporaryWorkers.SingleOrDefault(d => d.guid == TemporaryWorkerId);
        }
        //
        // Insert/Delete Methods
        public void Add(TemporaryWorkers dinner)
        {
            db.TemporaryWorkers.InsertOnSubmit(dinner);
        }
        public void Delete(TemporaryWorkers dinner)
        {
            db.TemporaryWorkers.DeleteOnSubmit(dinner);
        }

        //******************************RatedInfos****************************************
        // Insert/Delete Methods
        public void Add(RatedInfo dinner)
        {
            db.RatedInfo.InsertOnSubmit(dinner);
        }
        // Query Methods
        public IQueryable<RatedInfo> FindAllRatedInfos()
        {
            return db.RatedInfo;
        }
        public IQueryable<RatedInfo> GetRatedInfos(string RatedCode, string RatedName, string RatedType)
        {
            if (RatedType != null && RatedType != "")
            {
                return db.RatedInfo.Where(d => d.RatedType == RatedType && SqlMethods.Like(d.RatedCode, "%" + RatedCode + "%") && SqlMethods.Like(d.RatedName, "%" + RatedName + "%"));
            }
            return db.RatedInfo.Where(d => SqlMethods.Like(d.RatedCode, "%" + RatedCode + "%") && SqlMethods.Like(d.RatedName, "%" + RatedName + "%"));
        }
        public RatedInfo GetRatedInfo(int guid)
        {
            return db.RatedInfo.SingleOrDefault(d => d.RatedId == guid);
        }
        public void Delete(int guid)
        {
            db.RatedInfo.DeleteOnSubmit(GetRatedInfo(guid));
        }
        /// <summary>
        /// 检测定额号是否重复
        /// </summary>
        /// <param name="ratedCode"></param>
        /// <returns></returns>
        public bool ExistRatedCode(string ratedCode)
        {
            return db.RatedInfo.Where(d => d.RatedCode == ratedCode).Count() > 0;
        }
        /// <summary>
        /// 通过定额号获取定额名称
        /// </summary>
        /// <param name="ratedCode"></param>
        /// <returns></returns>
        public string GetRatedNameByRatedCode(string ratedCode)
        {
            var list = db.RatedInfo.Where(d => d.RatedCode == ratedCode).Select(d => d.RatedId + "#" + d.RatedName);
            if(list.Count()==0)return "";
            return list.First();
        }
        //******************************MaterialUseRecord****************************************
        // Insert/Delete Methods
        public void Add(MaterialUseRecord dinner)
        {
            db.MaterialUseRecord.InsertOnSubmit(dinner);
        }
        // Query Methods
        public IQueryable<MaterialUseRecord> FindAllMaterialUseRecords()
        {
            return db.MaterialUseRecord;
        }
        /// <summary>
        /// 获取材料使用清单
        /// </summary>
        /// <param name="projectId">项目ID</param>
        /// <param name="teamId">班组ID</param>
        /// <returns></returns>
        public IQueryable<MaterialUseRecord> FindMaterialUseRecordsByProjectIdTeamId(int projectId, int teamId)
        {
            if (projectId == 0)
                return db.MaterialUseRecord.Where(d => d.TeamId == teamId);
            return db.MaterialUseRecord.Where(d=>d.ProjectID == projectId && d.TeamId == teamId);
        }
        public IQueryable<MaterialUseRecord> GetMaterialUseRecords(string type, int foreignKey)
        {
            return db.MaterialUseRecord;
        }
        public MaterialUseRecord GeMaterialUseRecord(int guid)
        {
            return db.MaterialUseRecord.SingleOrDefault(d => d.id == guid);
        }
        public void Delete(MaterialUseRecord obj)
        {
            db.MaterialUseRecord.DeleteOnSubmit(obj);
        }
        public void DeleteMaterial(int guid)
        {
            db.MaterialUseRecord.DeleteOnSubmit(GeMaterialUseRecord(guid));
        }
        /// <summary>
        /// 绑定定额信息到班组
        /// </summary>
        /// <param name="ids">定额信息</param>
        /// <param name="adjustment">调整幅度</param>
        /// <param name="teamId">班组ID</param>
        /// <returns></returns>
        public bool SaveRatedInfosToGroup(string ids, int adjustment, int teamId)
        {
            string[] id_array = ids.Split(',');
            bool flag = true;
            //先删除上次保存过该班组的信息
            var existTeamRecord = FindMaterialUseRecordsByProjectIdTeamId(0, teamId);
            db.MaterialUseRecord.DeleteAllOnSubmit(existTeamRecord);
            //再新增本次该班组的信息
            var records = db.MaterialUseRecord.Where(d=>id_array.Contains(d.id+""));
            List<MaterialUseRecord> list = new List<MaterialUseRecord>();
            foreach (MaterialUseRecord mur in records)
            {
                MaterialUseRecord newMur = new MaterialUseRecord();
                newMur.CreateDate = DateTime.Now;
                newMur.CreateUserName = "admin";
                newMur.Labor_costs = mur.Labor_costs;
                newMur.Machinery_costs = mur.Machinery_costs;
                newMur.Management_Fee = mur.Management_Fee;
                newMur.Materials_costs = mur.Materials_costs;
                newMur.Profit = mur.Profit;
                newMur.Project_volume = mur.Project_volume;
                newMur.ProjectID = mur.ProjectID;
                newMur.RatedId = mur.RatedId;
                newMur.RatedName = mur.RatedName;
                newMur.TeamId = teamId;
                newMur.Single_costs = mur.Single_costs * Convert.ToDecimal(adjustment / 100.00);
                list.Add(newMur);
            }
            db.MaterialUseRecord.InsertAllOnSubmit(list);
            db.SubmitChanges();
            return flag;
        }
        //******************************AttachInfos****************************************
        // Query Methods
        public IQueryable<AttachInfo> FindAllAttachInfos()
        {
            return db.AttachInfo;
        }
        public IQueryable<AttachInfo> GetAttachInfos(string type, int foreignKey)
        {
            return db.AttachInfo.Where(d => d.type == type && d.ForeignKey == foreignKey);
        }
        public IQueryable<AttachInfo> GetAttachInfos(string type,string subtype, int foreignKey)
        {
            return db.AttachInfo.Where(d => d.type == type && d.subtype == subtype && d.ForeignKey == foreignKey);
        }
        public AttachInfo GetAttachInfo(int guid)
        {
            return db.AttachInfo.SingleOrDefault(d => d.guid == guid);
        }
        public void DeleteAttachInfo(int guid)
        {
            db.AttachInfo.DeleteOnSubmit(GetAttachInfo(guid));
        }
        public void DeleteAttachInfo(int foreignKey, string type)
        {
            db.AttachInfo.DeleteAllOnSubmit(GetAttachInfos(type, foreignKey));
        }
        //
        // Persistence
        public void Save()
        {
            db.SubmitChanges();
        }
    }
}